angular.module('meRecommendOrderListcontrollers', [])

  .controller('MeRecommendOrderListCtrl', function ($scope, $ionicActionSheet, $state, $timeout,$stateParams, Storage, MeRecommendOrderList,Loading,Toast,ENV,MeRecommendList,History) {

    var memberId = '';
    $scope.ENV = ENV;
    $scope.order = {
      orderName : ''
    };

    $scope.$on('$ionicView.beforeEnter', function () {
      memberId = Storage.get("member").member_id;
      $scope.wineList = MeRecommendOrderList.getSelectWineList();
    });

    //保存推荐酒单，保存到历史酒单中
    $scope.saveOrder = function(){
      //验证信息完整性
      if($scope.order.orderName == ""){
        Toast.showMessage("酒单名称不能为空");
      }else{
        var list = $scope.wineList;
        var wineIdList = [];
        for(var i=0;i<list.length;i++){
          wineIdList.push(list[i].wine_id+"");
        }
        Loading.showLoading();
        MeRecommendOrderList.saveOrderDetail(memberId,$scope.order.orderName,wineIdList);
        //清除界面信息
        $scope.order.orderName = "";
      }
    };

    //获取后台传来的结果
    $scope.$on('Me.saveOrderDetail', function () {
      Loading.hideLoading();
      var result = MeRecommendOrderList.getSaveOrderDetailJson();
      if(result.recommendStatus){
        var deleteWineIdList = [];
        for(i=0;i<$scope.wineList.length;i++){
          if($scope.wineList[i].mnw_id){
            deleteWineIdList.push($scope.wineList[i].mnw_id);
          }
        }
        if(deleteWineIdList && deleteWineIdList.length>0){
          MeRecommendList.deleteWine(memberId,deleteWineIdList);
        }
        $timeout(function(){
          $state.go('history-list');
        },1000);
      }
      Toast.showMessage(result.recommendMsg);
    });

    //分享酒单
    $scope.shareOrder = function () {
      //判断酒单名称是否为空
      if($scope.order.orderName == ""){
        Toast.showMessage("酒单名称不能为空");
        /*$ionicLoading.show({
          template: '酒单名称不能为空',
          duration : 2000
        });*/
      }else{
        //进行分享操作，待完善
      }
    };

    //改变数字的显示，添加逗号分割
    $scope.getFormatNum = function (num) {
      return formatNum(num);
    };
    //返回
    $scope.goback = function(){
      History.goback();
    }

  });
